/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmCloseAppointments.java
 *
 * Arthur mbelisle age 24
 * Created on Jul 20, 2011, 10:22:48 AM
 */
package khmhappointmentsystem;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author mbelisle
 */
public class FrmCloseAppointments extends javax.swing.JInternalFrame
{

    static final Logger logger = Logger.getLogger(FrmCloseAppointments.class.getName());

    /** Creates new form FrmCloseAppointments */
    public FrmCloseAppointments()
    {
        initComponents();
        setCalendarDate();
        loadClinic();
        loadSpecialist();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cmbClinic = new javax.swing.JComboBox();
        cmdSpecialist = new javax.swing.JComboBox();
        calDate = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        cmdExit = new javax.swing.JButton();
        cmdClose = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(khmhappointmentsystem.Main.class).getContext().getResourceMap(FrmCloseAppointments.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        cmbClinic.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbClinic.setName("cmbClinic"); // NOI18N

        cmdSpecialist.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmdSpecialist.setName("cmdSpecialist"); // NOI18N

        calDate.setName("calDate"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(khmhappointmentsystem.Main.class).getContext().getActionMap(FrmCloseAppointments.class, this);
        cmdExit.setAction(actionMap.get("Exit")); // NOI18N
        cmdExit.setIcon(resourceMap.getIcon("cmdExit.icon")); // NOI18N
        cmdExit.setText(resourceMap.getString("cmdExit.text")); // NOI18N
        cmdExit.setName("cmdExit"); // NOI18N

        cmdClose.setAction(actionMap.get("Close")); // NOI18N
        cmdClose.setIcon(resourceMap.getIcon("cmdClose.icon")); // NOI18N
        cmdClose.setText(resourceMap.getString("cmdClose.text")); // NOI18N
        cmdClose.setName("cmdClose"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(calDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmdSpecialist, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbClinic, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(cmdClose)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdExit)))))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbClinic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmdSpecialist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(calDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdExit)
                    .addComponent(cmdClose))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-277)/2, (screenSize.height-210)/2, 277, 210);
    }// </editor-fold>//GEN-END:initComponents

    void setCalendarDate()
    {
        calDate.setDate(new java.util.Date());
    }

    private void loadClinic()
    {
        cmbClinic.removeAllItems();
        cmbClinic.addItem(" ");
        String query = "SELECT DISTINCT name FROM clinic";
        try
        {
            PreparedStatement prep = Environment.getDbConn().prepareStatement(query);
            ResultSet rs = prep.executeQuery();

            while (rs.next())
            {
                cmbClinic.addItem(rs.getString(1));
            }
            rs.close();
            prep.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void loadSpecialist()
    {
        cmdSpecialist.removeAllItems();
        cmdSpecialist.addItem(" ");
        String query = "SELECT DISTINCT specialist FROM clinic";
        try
        {
            PreparedStatement prep = Environment.getDbConn().prepareStatement(query);
            ResultSet rs = prep.executeQuery();

            while (rs.next())
            {
                cmdSpecialist.addItem(rs.getString(1));
            }
            rs.close();
            prep.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Action
    public void Exit()
    {
        this.dispose();
    }

    @Action
    public void Close()
    {
        DateFormat MDY_Formatter = new SimpleDateFormat("MM-dd-yyyy");
        String newDate = MDY_Formatter.format(calDate.getDate());
        String doctor = cmdSpecialist.getSelectedItem().toString();
        String clinic = cmbClinic.getSelectedItem().toString();

        try
        {
            //Copy all records from appointments table that has been checkin and place records in Appointment_History table
            String query = "INSERT INTO Appointment_History "
                    + "SELECT DISTINCT * "
                    + "FROM Appointments "
                    + "Where clinic LIKE '" + clinic + "' AND specialist LIKE '" + doctor + "' AND "
                    + "date LIKE '" + newDate + "'";
            PreparedStatement pstmt = Environment.getDbConn().prepareStatement(query);
            pstmt.executeUpdate();

            String delete = "DELETE From Appointments "
                    + "WHERE (checkin like 'y') AND clinic LIKE '" + clinic + "' AND specialist LIKE '" + doctor + "' AND "
                    + "date LIKE '" + newDate + "'";
            PreparedStatement stmt = Environment.getDbConn().prepareStatement(delete);
            stmt.executeUpdate();
            JOptionPane.showMessageDialog(rootPane, "Successfully closed appointment period.");
        }
        catch (Exception e)
        {
            String message = "An error occurred while closing appointment period.";
            logger.log(Level.SEVERE, message, e);
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser calDate;
    private javax.swing.JComboBox cmbClinic;
    private javax.swing.JButton cmdClose;
    private javax.swing.JButton cmdExit;
    private javax.swing.JComboBox cmdSpecialist;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
}
